package model.controller.UserController;

import model.controller.ControllerException;
import model.user.User;

import java.sql.*;

public class UserDataAccesObject {
    Statement stmt = null;
    Connection conn = null;
    ResultSet r = null;
    PreparedStatement pstmt = null;

    public UserDataAccesObject() {
        
    }
        
    public void addUser(User user) throws ControllerException {
        try{
            connectToDatabase();
            
            stmt = conn.createStatement();
            stmt.execute("SELECT * FROM USERS WHERE LOGIN='" + user.getLogin() + "'");
            r = stmt.getResultSet();

            if (r.next()) {            
                throw new ControllerException("Such login has alredy exist");
            }

            pstmt = conn.prepareStatement("INSERT INTO Users (login,password,status)" +
                    " VALUES ('"+user.getLogin()+"','"+user.getPassword()+"','"+user.getStatus()+"')");
            pstmt.execute();

        } catch (SQLException e) {
            e.printStackTrace();    
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            disconnect(stmt, conn);
        }

    }

    private void connectToDatabase() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Races", "root", "11111");
    }

    private void disconnect(Statement stmt, Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void checkIfUserExist(User user) throws ControllerException {
        try {
            connectToDatabase();

            stmt = conn.createStatement();
            stmt.execute("SELECT * FROM USERS WHERE LOGIN = '" + user.getLogin() +
                    "' AND PASSWORD='" + user.getPassword() + "'");
            r = stmt.getResultSet();

            if(!r.next()) {
                throw new ControllerException("No such login/password combination found");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            disconnect(stmt, conn);
        }
    }
    
    public String checkStatus(User user) {
        try {
            connectToDatabase();

            stmt = conn.createStatement();
            stmt.execute("SELECT * FROM USERS WHERE LOGIN = '" + user.getLogin() +
                    "' AND PASSWORD='" + user.getPassword() + "'");
            r = stmt.getResultSet();

            r.next();

            return r.getObject("status").toString();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            disconnect(stmt, conn);
        }

        return null;
    }
}
